package oiwiki;

import java.util.Scanner;

/**
 * @Classname LargestSquare
 * @Description TODO
 * @Date 2021/9/13 15:14
 * @Created by ASUS
 * 在一个n*m 的只包含0 和1 的矩阵里找出一个不包含0 的最大正方形，输出边长。
 */
public class LargestSquare {
    public static int[][] getMatrix() {
        Scanner scanner = new Scanner(System.in);
        String[] s = scanner.nextLine().split(" ");
        int n = Integer.parseInt(s[0]), m = Integer.parseInt(s[1]);
        int[][] matrix = new int[n][m];
        for (int i = 0; i < n; i++) {
            s = scanner.nextLine().split(" ");
            for (int j = 0; j < m; j++) {
                matrix[i][j] = Integer.parseInt(s[j]);
            }
        }
        return matrix;
    }

    public static void main(String[] args) {
        int[][] matrix = getMatrix();
        int max = 0;
        int len = Math.min(matrix.length, matrix[0].length);
        for (int i = 0; i < len; i++) {
            if ((i+1) * (i+1) == sum(matrix, i, i)) max = Math.max(max, i + 1);
        }
        System.out.println(max);
    }

    static int sum(int[][] matrix, int x, int y) {
        if (x == 0 && y == 0) return matrix[0][0];
        if (x == 0) {
            return matrix[x][y] + sum(matrix, x, y - 1);
        } else {
            if (y == 0) return matrix[x][y] + sum(matrix, x - 1, y);
            else return matrix[x][y] + sum(matrix, x - 1, y) + sum(matrix, x, y - 1) - sum(matrix, x-1,y-1);
        }
    }
}
